/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sd_proj1.mainProgram;

import java.util.TreeMap;
import sd_proj1.monSolution.FifoNode;
import sd_proj1.interfaces.IDonaLoja;
import sd_proj1.interfaces.IDonaOficina;
import sd_proj1.interfaces.IntDonaFifo;
import sd_proj1.monSolution.Mensagem;
import sd_proj1.monSolution.Relogio;

/**
 *
 * @author PedroEee
 */
public class DonaLoja extends Thread {

	private IDonaLoja loja;
	private IDonaOficina oficina;
	private IntDonaFifo rfifo;
	private FifoNode obj;
	private TreeMap map;

	public DonaLoja(IDonaLoja loja, IDonaOficina oficina, IntDonaFifo rfifo, TreeMap map) {
		this.loja = loja;
		this.oficina = oficina;
		this.rfifo = rfifo;
		this.map = map;
	}

	@Override
	public void run() {
		int c;                                                     /* customer id */
		char nextTask = '0';                                  /* task to be carried out */
		boolean busy;                               /* in-the-shop signaling flag
		TRUE if the entrepreneur is in the shop */
		while (!loja.endOperEntrep()) {
			loja.prepareToWork();                    /* the entrepreneur opens the shop
			and gets ready to perform her duties */
			busy = (true);                                           /* set busy flag */
			while (busy) {
				obj = rfifo.readRequest();
//              System.out.println("Cenas - appSit: " + obj.appraiseSit() + " - addCus: " + obj.addressACostumer());
				nextTask = obj.appraiseSit();
				System.out.println("A dona lê a tarefa  " + nextTask);
				if (nextTask == 'C') /* the entrepreneur checks if a
				customer is needing attention */ {
					c = obj.addressACostumer(); /* the entrepreneur goes to the counter
					to attend a customer */
					System.out.println("O cliente a ser atendido primeiro e' " + c);
					serviceCustomer();    /* the entrepreneur services the customer */
					loja.sayGoodByeToCustomer(c);          /* the entrepreneur completes
					the transaction */
				} else {
					busy = (false);                            /* reset busy flag */
					/* the entrepreneur checks if some craftsman has phoned him */
					if ((nextTask == 'G') || (nextTask == 'P')) {
						if (loja.customersInTheShop()) {
							loja.closeTheDoor();   /* the entrepreneur closes the
							door to prevent further customers to enter */
							busy = (true);                     /* set busy flag */
						}
					}
				}
			}
			loja.prepareToLeave();                    /* the entrepreneur closes the shop */
			if (nextTask == 'G') {
				oficina.goToWorkshop();         /* the entrepreneur collects a batch of goods */
			} else if (nextTask == 'P') {
				oficina.visitSuppliers(); /* the entrepreneur goes shopping for prime
				materials and delivers them to the workshop */
			}
			
			loja.returnToShop();                /* the entrepreneur goes back to the shop */
		}

	}

	private void serviceCustomer() {
//		Mensagem mlog = new Mensagem();
		try {
			sleep((long) (5 + 10 * Math.random()));
		} catch (InterruptedException e) {
		}
//		mlog.setEntrepStat(Mensagem.eStat.ATAC);
//		map.put(Relogio.getInstance().getValue(), mlog);
		System.out.println("Atendendo um cliente");
	}
}

